package com.yupi.yuoj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.yuoj.annotation.AuthCheck;
import com.yupi.yuoj.common.BaseResponse;
import com.yupi.yuoj.common.ErrorCode;
import com.yupi.yuoj.common.ResultUtils;
import com.yupi.yuoj.constant.UserConstant;
import com.yupi.yuoj.exception.BusinessException;
import com.yupi.yuoj.exception.ThrowUtils;
import com.yupi.yuoj.model.dto.populartags.PopularTagsQueryRequest;
import com.yupi.yuoj.model.entity.PopularTags;
import com.yupi.yuoj.model.vo.PopularTagsVO;
import com.yupi.yuoj.service.PopularTagsService;
import com.yupi.yuoj.service.UserService;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 热门标签接口
 *
 * @author jingjin
 */
@RestController
@RequestMapping("/popular_tags")
@Slf4j
public class PopularTagsController {

    @Resource
    private PopularTagsService popularTagsService;

    @Resource
    private UserService userService;

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<PopularTags> getPopularTagsById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        PopularTags popularTags = popularTagsService.getById(id);
        if (popularTags == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(popularTags);
    }

    /**
     * 分页获取热门标签列表（仅管理员）
     *
     * @param popularTagsQueryRequest
     * @return
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<PopularTags>> listPopularTagsByPage(@RequestBody PopularTagsQueryRequest popularTagsQueryRequest) {
        long current = popularTagsQueryRequest.getCurrent();
        long size = popularTagsQueryRequest.getPageSize();
        Page<PopularTags> popularTagsPage = popularTagsService.page(new Page<>(current, size),
                popularTagsService.getQueryWrapper(popularTagsQueryRequest));
        return ResultUtils.success(popularTagsPage);
    }

    /**
     * 分页获取热门标签列表（封装类）
     *
     * @param popularTagsQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<PopularTagsVO>> listPopularTagsVOByPage(@RequestBody PopularTagsQueryRequest popularTagsQueryRequest,
            HttpServletRequest request) {
        long current = popularTagsQueryRequest.getCurrent();
        long size = popularTagsQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<PopularTags> popularTagsPage = popularTagsService.page(new Page<>(current, size),
                popularTagsService.getQueryWrapper(popularTagsQueryRequest));
        return ResultUtils.success(popularTagsService.getPopularTagsVOPage(popularTagsPage, request));
    }

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<PopularTagsVO> getPopularTagsVOById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        PopularTags popularTags = popularTagsService.getById(id);
        if (popularTags == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(popularTagsService.getPopularTagsVO(popularTags, request));
    }

}